package org.leumasjaffe.initiativeTracker.distributed.controller;

import static org.leumasjaffe.initiativeTracker.operator.InitiativeController.tracker;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.leumasjaffe.initiativeTracker.Constants;
import org.leumasjaffe.initiativeTracker.operator.CharacterLinker;
import org.leumasjaffe.initiativeTracker.operator.InitiativeController;
import org.leumasjaffe.initiativeTracker.view.InitiativeFrame;
import org.w3c.dom.NamedNodeMap;

import edu.wpi.heineman.xml.Message;
import edu.wpi.heineman.client.IMessageHandler;

@NonNullByDefault
public class KillAllHandler implements IMessageHandler {

	@Override
	public void process(Message response) {
		NamedNodeMap map = response.contents.getFirstChild().getAttributes();
		
		boolean all = Boolean.parseBoolean(map.getNamedItem("all").getNodeValue());
		
		@SuppressWarnings("null")
		InitiativeController tracker = tracker(map.getNamedItem("game").getNodeValue());
		
		if (all) {
			Constants.pageNum = 0;
			tracker.root.killAll();
			System.out.println("\nAll NPC entities removed...\nTurn values reset," +
					"id counter preserved.\n");
		}
		else {
			for (CharacterLinker c : tracker.list.getList())
				c.tearDown();
			tracker.list.clear();
			tracker.root.gen.setId(0);
			System.out.println("\nAll entities removed...\nAll fields and" +
					"counters reset to default.\n");
		}
		if (Constants.client)
			InitiativeFrame.getInstance().reset();
	}

	@Override
	public IMessageHandler newCopy() {
		return new KillAllHandler();
	}

}
